package com.tovast.demo.controller;

import com.tovast.demo.domain.Attendance;
import com.tovast.demo.domain.User;
import com.tovast.demo.repository.AttendanceRepostiroy;
import com.tovast.demo.repository.UserRepostiroy;
import com.tovast.demo.service.AttendanceService;
import com.tovast.demo.service.SecurityMangeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * Created by zjp on 2016/9/24.
 */
@Controller
@RequestMapping("attendance")
public class AttendanceController {
    @Autowired
    private AttendanceService attendanceService;
    @Autowired
    private AttendanceRepostiroy attendanceRepostiroy;


    @RequestMapping(method = RequestMethod.GET)
    @ResponseBody
    public Page<VAttendance> list(@RequestParam("name") String name, Pageable pageable){
       Page<Attendance> ats=  attendanceRepostiroy.findAllByUserNameContaining(name,pageable);
        return new PageImpl<VAttendance>(VAttendance.convert(ats.getContent()),pageable,ats.getTotalElements());
    }
    @RequestMapping(method = RequestMethod.POST)
    @ResponseBody
    public VAttendance registe(@RequestBody VAttendance attendance){
//        attendanceService.registe(attendance);
        return attendance;
    }
    @RequestMapping(value = "{id}" ,method = RequestMethod.GET)
    @ResponseBody
    public Attendance findOne(@PathVariable("id") int id){

        return attendanceRepostiroy.findOne(id);
    }
    @RequestMapping(value = "{id}" ,method = RequestMethod.DELETE)
    @ResponseBody
    public Attendance delete(@PathVariable("id") int id){
        Attendance attendance= attendanceRepostiroy.findOne(id);
        attendanceService.destroy(attendance);
        return attendance;
    }
    @RequestMapping(value = "{id}" ,method = RequestMethod.PUT)
    @ResponseBody
    public VAttendance update(@PathVariable("id") Attendance old, @RequestBody VAttendance user){
            old.setDescription(user.getDescription());
        old.setInTime(user.getInTime());
        old.setOutTime(user.getOutTime());
        attendanceRepostiroy.saveAndFlush(old);
        return VAttendance.builder().attendance(old).build();

    }

}
